﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCM.Logic;
using DataAccess.DbHelper;
using SubSonic.Query;
using DataModel;
using Common;

namespace Web.Accounting
{
    public partial class PaymentList : System.Web.UI.Page
    {
        List<string> _order = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            _order = new List<string>();
            _order.Add(View_Reconciliation.InsTimeColumn);

            if (Page.IsPostBack == false)
            {
                BindDataGrid();
            }
        }
        private void BindDataGrid()
        {
            List<ConditionHelper.SqlqueryCondition> wheres = InquiryCondition();
            View_ReconciliationBll.GetInstence().BindGrid(GridResult, GridResult.PageIndex + 1, GridResult.PageSize, wheres, _order);
        }


        private List<ConditionHelper.SqlqueryCondition> InquiryCondition()
        {
            var list = new List<ConditionHelper.SqlqueryCondition>();

            //SupplierCode
            list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_Reconciliation.SupplierCodeColumn, Comparison.Equals, Context.User.Identity.Name));

            //单据状态
            //if (!string.IsNullOrEmpty(ddlStatus.SelectedValue))
            //{
            //    list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_Reconciliation.StatusCodeColumn, Comparison.Equals, ConvertHelper.Cint0(ddlStatus.SelectedValue)));
            //}
            ////起始时间
            //if (!string.IsNullOrEmpty(DatePickerStartDate.Text))
            //{
            //    list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_Reconciliation.InsTimeColumn, Comparison.GreaterOrEquals, StringHelper.FilterSql(DatePickerStartDate.Text)));
            //    //终止时间
            //    if (!string.IsNullOrEmpty(DatePickerEndDate.Text))
            //    {
            //        list.Add(new ConditionHelper.SqlqueryCondition(ConstraintType.And, View_Reconciliation.InsTimeColumn, Comparison.LessOrEquals, StringHelper.FilterSql(DatePickerEndDate.Text)));
            //    }
            //}
            return list;
        }

        protected void GridResult_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            //处理排序
            _order = null;
            _order = new List<string>();
            if (e.SortField.Length > 0)
            {
                //判断是升序还是降序
                if (StringHelper.Upper(e.SortDirection) == "DESC")
                {
                    _order.Add(e.SortField + " desc");
                }
                else
                {
                    _order.Add(e.SortField + " asc");
                }
                //判断排序关键字是否为主键，否的话加上主键
                if (e.SortField.ToLower() != View_Reconciliation.ReconciliationNoColumn.ToLower())
                {
                    _order.Add(View_Reconciliation.ReconciliationNoColumn + " desc");
                }
            }
            else
            {
                _order.Add(View_Reconciliation.ReconciliationNoColumn + " desc");
            }

            GridResult.SortColumn = e.SortField;
            GridResult.SortColumnIndex = e.ColumnIndex;
            BindDataGrid();
        }

        protected void GridResult_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            GridResult.PageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        public string GetStatusName(object intStatusCode)
        {
            if (intStatusCode == null)
            {
                return string.Empty;
            }
            var reconciliationSatus = M_ReconciliationStatus.SingleOrDefault(x => x.ReconciliationStatusId == Convert.ToInt32(intStatusCode));
            if (reconciliationSatus == null)
            {
                return string.Empty;
            }
            else
                return reconciliationSatus.StatusName;
        }

        protected void WindowInvoice_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {
            //刷新页面
            BindDataGrid();
        }

    }
}
